package cn.innosmart.aq.util;

import java.util.Comparator;

/**
 * Created by David on 2015/8/23.
 */
public class StringListSortUtil implements Comparator<String> {


    private final static int UP = 1;    //升序

    private final static int DOWM = -1; //降序

    private int sortVal;                //选择的排序方式

    private String sortKey = "";        //按某字段进行排序

    public StringListSortUtil() {

    }

    public StringListSortUtil(int sortVal) {
        this.sortVal = sortVal;
    }

    @Override
    public int compare(String o1, String o2) {
        if (sortVal == StringListSortUtil.UP) {
            return sortUp(o1, o2);
        } else if (sortVal == StringListSortUtil.DOWM) {
            return sortDown(o1, o2);
        }
        return 0;
    }

    //升序
    private int sortUp(String o1, String o2) {
        int val1 = Integer.valueOf(o1);
        int val2 = Integer.valueOf(o2);
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }
    }

    //降序
    private int sortDown(String o1, String o2) {
        int val1 = Integer.valueOf(o1);
        int val2 = Integer.valueOf(o2);
        if (val1 > val2) {
            return -1;
        } else if (val1 < val2) {
            return 1;
        } else {
            return 0;
        }
    }

}
